<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>今日尾盘推荐</title>
    <link href="{{ url_for('static', path='css/bootstrap.min.css') }}" rel="stylesheet">
    <link href="{{ url_for('static', path='css/bootstrap.rtl.min.css') }}" rel="stylesheet">
    <link href="{{ url_for('static', path='css/bootstrap-grid.min.css') }}" rel="stylesheet">
    <link href="{{ url_for('static', path='css/bootstrap-grid.rtl.min.css') }}" rel="stylesheet">
    <link href="{{ url_for('static', path='css/bootstrap-reboot.min.css') }}" rel="stylesheet">
    <link href="{{ url_for('static', path='css/bootstrap-reboot.rtl.min.css') }}" rel="stylesheet">
    <link href="{{ url_for('static', path='css/bootstrap-utilities.min.css') }}" rel="stylesheet">
    <link href="{{ url_for('static', path='css/bootstrap-utilities.rtl.min.css') }}" rel="stylesheet">
    <script src="{{ url_for('static', path='js/bootstrap.bundle.min.js') }}"></script>
    <script src="{{ url_for('static', path='js/bootstrap.esm.min.js') }}"></script>
    <script src="{{ url_for('static', path='js/bootstrap.min.js') }}"></script>
</head>
<body>
<div class="container-fluid">
    <nav>
        <div class="nav nav-tabs" id="nav-tab" role="tablist">
            <button class="nav-link active"
                    id="wei-pan-suggest-tab"
                    data-bs-toggle="tab"
                    data-bs-target="#wei-pan-suggest"
                    type="button"
                    role="tab"
                    aria-controls="wei-pan-suggest"
                    aria-selected="true">
                尾盘推荐
            </button>
            <button class="nav-link"
                    id="shouban-suggest-tab"
                    data-bs-toggle="tab"
                    data-bs-target="#shouban-suggest"
                    type="button"
                    role="tab"
                    aria-controls="shouban-suggest"
                    aria-selected="false">
                首板推荐
            </button>
            <button class="nav-link"
                    id="shouban-yesterday-tab"
                    data-bs-toggle="tab"
                    data-bs-target="#shouban-yesterday"
                    type="button"
                    role="tab"
                    aria-controls="shouban-yesterday"
                    aria-selected="false">
                昨天首板
            </button>

        </div>
    </nav>
    <div class="tab-content" id="nav-tabContent">
        <div class="tab-pane fade show active"
             id="wei-pan-suggest"
             role="tabpanel"
             aria-labelledby="nav-home-tab"
             tabindex="0">
            <table class="table table-striped" id="wei-pan">
                <thead>
                <tr>
                    <th>代码</th>
                    <th>名称</th>
                    <th>价格</th>
                    <th>涨跌幅</th>
                    <th>振幅</th>
                    <th>开盘价</th>
                    <th>最高价</th>
                    <th>最低价</th>
                    <th>成交额</th>
                    <th>换手率</th>
                    <th>总市值</th>
                    <th>流通市值</th>
                    <th>较昨日放量</th>
                    <th>上影线幅度</th>
                </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
        <div class="tab-pane fade"
             id="shouban-suggest"
             role="tabpanel"
             aria-labelledby="nav-profile-tab"
             tabindex="0">
            <table class="table" id="zt">
                <thead>
                <tr>
                    <th>股票代码</th>
                    <th>股票名称</th>
                    <th>价格</th>
                    <th>涨跌幅</th>
                    <th>流通市值</th>
                    <th>总市值</th>
                    <th>换手率</th>
                    <th>行业板块</th>
                    <th>连板数</th>
                    <th>首次封板时间</th>
                    <th>最后封板时间</th>
                    <th>成交额</th>
                    <th>封板资金</th>
                    <th>炸板次数</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
        <div class="tab-pane fade"
             id="shouban-yesterday"
             role="tabpanel"
             aria-labelledby="nav-profile-tab"
             tabindex="0">
            <table class="table" id="shouban-yesterday-zt">
                <thead>
                <tr>
                    <th>日期</th>
                    <th>股票代码</th>
                    <th>股票名称</th>
                    <th>价格</th>
                    <th>涨跌幅</th>
                    <th>振幅</th>
                    <th>开盘价</th>
                    <th>最低价</th>
                    <th>最高价</th>
                    <th>成交量</th>
                    <th>成交额</th>
                    <th>流通市值</th>
                    <th>总市值</th>
                    <th>换手率</th>
                </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
    </div>

</div>

</body>
<script>
    const tabEl = document.querySelectorAll('button[data-bs-toggle="tab"]')
    var sb_interval, yesterday_zt_interval;
    tabEl.forEach((e) => {
        e.addEventListener('shown.bs.tab', event => {
            switch (event.target.id) {
                case  "wei-pan-suggest-tab":
                    clearInterval(sb_interval)
                    clearInterval(yesterday_zt_interval)
                    wp_refresh()
                    break
                case "shouban-suggest-tab":
                    clearInterval(yesterday_zt_interval)
                    sb_refresh()
                    sb_interval = setInterval(sb_refresh, 2000)
                    break
                case "shouban-yesterday-tab":
                    clearInterval(sb_interval)
                    yesterday_zt()
                    yesterday_zt_interval = setInterval(yesterday_zt, 1000)
                    break
            }
        })
    })


    const suggest_keys = [
        "symbol", "name", "price", "change_rate", "diff_rate", "open_price", "highest_price", "lowest_price", "trade_amount",
        "change_hand_rate", "market_value", "l_market_value", "vr", "up_line"
    ]
    const zt_keys = [
        "symbol", "name", "price", "change_rate", "l_market_value", "market_value", "change_hand_rate",
        "hybk", "lbc", "fbt", "lbt", "trade_amount", "fund", "zbc", "operate"
    ]

    const yesterday_zt_keys = [
        "date", "symbol", "name", "price", "change_rate", "diff_rate", "open_price", "lowest_price", "highest_price", "volume",
        "trade_amount", "l_market_value", "market_value", "change_hand_rate"
    ]


    wp_refresh()

    function wp_refresh() {
        add_loading(suggest_keys.length)
        suggest_stock()
    }


    function sb_refresh() {
        // add_loading(zt_keys.length)
        shouban()

    }


    function refresh() {
        let st = document.querySelector("#switcher")
        if (st.checked) {
            wp_refresh()
        } else {
            sb_refresh()
        }
    }

    function change_frame(e) {
        let tb_wb = document.querySelector("#wei-pan")
        let tb_zt = document.querySelector("#zt")
        // let topic = document.querySelector("#topic")
        if (e.value === "0") {
            // topic.innerText = "尾盘推荐"
            tb_wb.className = tb_wb.className.split(" ").filter(e => e !== "d-none").join(" ")
            tb_zt.className = tb_zt.className.split(" ").concat(['d-none']).join(" ")
            wp_refresh()
        } else if (e.value === "1") {
            // topic.innerText = "首板推荐"
            tb_wb.className = tb_wb.className.split(" ").concat(['d-none']).join(" ")
            tb_zt.className = tb_zt.className.split(" ").filter(e => e !== "d-none").join(" ")
            sb_refresh()
        }


    }

    function clear_table() {
        document.querySelectorAll("table tbody").forEach((e) => {
            e.innerHTML = ``
        })
    }

    function add_loading(length) {
        document.querySelectorAll("table tbody").forEach((e) => {
            e.innerHTML = `<tr>
        <td colspan="${length}" class="text-center">
            <div class="spinner-border" role="status">
                <span class="visually-hidden">Loading...</span>
            </div>
                </td>
            </tr>`
        })
    }

    function ma_html(e, key, num) {
        let ma = e[`ma${num}`]
        if (e[key] > ma) {
            return `<span class="text-danger" title="${ma}">${num}</span>`
        } else if (e[key] < ma) {
            return `<span class="text-success" title="${ma}">${num}</span>`
        } else {
            return `<span class="text-secondary" title="${ma}">${num}</span>`
        }
    }

    function max_val_html(e, key, num) {
        let mv = e[`max_val${num}`]
        if (e[key] > mv) {
            return `<span class="text-danger" title="${(mv / 100000000).toFixed(2)}亿">${num}</span>`
        } else if (e[key] < mv) {
            return `<span class="text-success" title="${(mv / 100000000).toFixed(2)}亿">${num}</span>`
        } else {
            return `<span class="text-secondary" title="${(mv / 100000000).toFixed(2)}亿">${num}</span>`
        }
    }

    function suggest_stock() {
        fetch(`/dongcai/api/suggest`).then(resp => resp.json()).then(data => {
            clear_table()
            let tb = document.querySelector("#wei-pan tbody")
            data.forEach(e => {
                let tr = document.createElement("tr")
                tb.append(tr)
                suggest_keys.forEach(key => {
                    let td = document.createElement("td")
                    tr.append(td)
                    if (["highest_price", "lowest_price", "vr"].indexOf(key) !== -1) {
                        td.innerText = e[key].toFixed(2)
                    } else if (["change_rate", "diff_rate", "change_hand_rate", "up_line"].indexOf(key) !== -1) {
                        td.innerText = `${e[key].toFixed(2)}%`
                    } else if (["market_value", "l_market_value"].indexOf(key) !== -1) {
                        td.innerText = `${(e[key] / 100000000).toFixed(2)}亿`
                    } else if (["price", "open_price"].indexOf(key) !== -1) {
                        let _html = e[key].toFixed(2) + "("
                        _html += ma_html(e, key, "5") + "|"
                        _html += ma_html(e, key, "10") + "|"
                        _html += ma_html(e, key, "15") + "|"
                        _html += ma_html(e, key, "20") + ")"
                        td.innerHTML = _html
                    } else if (["trade_amount"].indexOf(key) !== -1) {
                        let _html = `${(e[key] / 100000000).toFixed(2)}亿` + "("
                        _html += max_val_html(e, key, "5") + "|"
                        _html += max_val_html(e, key, "10") + "|"
                        _html += max_val_html(e, key, "15") + "|"
                        _html += max_val_html(e, key, "20") + ")"
                        td.innerHTML = _html
                    } else {
                        td.innerText = e[key]
                    }
                })
            })
        })
    }


    function shouban() {
        fetch(`/dongcai/api/zt`).then(resp => resp.json()).then(data => {
                clear_table()
                let tb = document.querySelector("#zt tbody")
                data.forEach(e => {
                    let tr = document.createElement("tr")
                    tb.append(tr)
                    if (e.diff_amount / e.fund > 0.2) {
                        tr.className = "table-danger"
                    }

                    zt_keys.forEach(key => {
                        let td = document.createElement("td")
                        tr.append(td)
                        if (["price"].indexOf(key) !== -1) {
                            console.log(key)
                            td.innerText = e[key].toFixed(2)
                        } else if (["change_rate", "change_hand_rate"].indexOf(key) !== -1) {
                            td.innerText = `${e[key].toFixed(2)}%`
                        } else if (["market_value", "l_market_value"].indexOf(key) !== -1) {
                            td.innerText = `${(e[key] / 100000000).toFixed(2)}亿`
                        } else if (["trade_amount"].indexOf(key) !== -1) {
                            let i
                            if (e.diff_amount > 0) {
                                i = `<span class="text-danger">${(e.diff_amount / 10000).toFixed(2)}</span>`
                            } else if (e.diff_amount < 0) {
                                i = `<span class="text-success">${(e.diff_amount / 10000).toFixed(2)}</span>`
                            } else {
                                i = `<span class="text-secondary">${(e.diff_amount / 10000).toFixed(2)}</span>`
                            }
                            td.innerHTML = `${(e[key] / 10000).toFixed(0)}(${i})`
                        } else if (["fund"].indexOf(key) !== -1) {
                            let i
                            if (e.diff_fund > 0) {
                                i = `<span class="text-danger">${(e.diff_fund / 10000).toFixed(2)}</span>`
                            } else if (e.diff_fund < 0) {
                                i = `<span class="text-success">${(e.diff_fund / 10000).toFixed(2)}</span>`
                            } else {
                                i = `<span class="text-secondary">${(e.diff_fund / 10000).toFixed(2)}</span>`
                            }
                            td.innerHTML = `${(e[key] / 10000).toFixed(0)}(${i})`
                        } else if (["operate"].indexOf(key) !== -1) {
                            btn = document.createElement("button")
                            btn.className = "btn btn-primary btn-sm"
                            btn.innerText = "盯"
                            btn.onclick = () => {
                                fetch("/dongcai/api/zt_record", {
                                    body: JSON.stringify(e),
                                    method: "POST",
                                    headers: {"content-type": "application/json"}
                                })
                            }
                            td.append(btn)
                        } else {
                            td.innerText = e[key]
                        }


                    })
                })
            }
        )
    }

    function yesterday_zt() {
        fetch(`/dongcai/api/yesterday_zt`).then(resp => resp.json()).then(data => {
                clear_table()
                let tb = document.querySelector("#shouban-yesterday-zt tbody")
                data.forEach(e => {
                    let tr = document.createElement("tr")
                    tb.append(tr)
                    if (e.diff_amount / e.fund > 0.2) {
                        tr.className = "table-danger"
                    }

                    yesterday_zt_keys.forEach(key => {
                        let td = document.createElement("td")
                        tr.append(td)
                        if (["highest_price", "lowest_price"].indexOf(key) !== -1) {
                            td.innerText = e[key].toFixed(2)
                        } else if (["price", "open_price"].indexOf(key) !== -1) {
                            let _html = e[key].toFixed(2) + "("
                            _html += ma_html(e, key, "5") + "|"
                            _html += ma_html(e, key, "10") + "|"
                            _html += ma_html(e, key, "15") + "|"
                            _html += ma_html(e, key, "20") + ")"
                            td.innerHTML = _html
                        } else if (["diff_rate", "change_hand_rate"].indexOf(key) !== -1) {
                            td.innerText = `${e[key].toFixed(2)}%`
                        } else if (["change_rate"].indexOf(key) !== -1) {
                            if (e[key] > 0)
                                td.innerHTML = `<span class="text-danger">${e[key].toFixed(2)}%</span>`
                            else if (e[key]<0)
                                td.innerHTML = `<span class="text-success">${e[key].toFixed(2)}%</span>`
                            else
                                td.innerHTML = `<span class="text-secondary">${e[key].toFixed(2)}%</span>`
                        } else if (["market_value", "l_market_value"].indexOf(key) !== -1) {
                            td.innerText = `${(e[key] / 100000000).toFixed(2)}亿`
                        } else {
                            td.innerText = e[key]
                        }


                    })
                })
            }
        )
    }
</script>
</html>